package com.wno704.quartz.task;

import com.wno704.quartz.entity.Job;
import com.wno704.quartz.service.JobService;
import com.wno704.system.util.HttpUtil;
import com.wno704.system.util.HttpsUtil;
import com.wno704.system.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName HttpTask
 * @Description TODO
 * @Author wno704
 * @Date 2021/12/12 19:33
 * @Version 1.0
 */
@Slf4j
@Component
public class HttpTask {

    @Autowired
    private JobService jobService;

    public void httpSend(String params) throws IOException {
        Job job = jobService.findJob(Long.parseLong(params));
        log.info("{}-{}----start", job.getRemark(), "httpSend");
        log.info("{}----{}----{}----{}----{}----{}----{}", params, job.getTemplateSrcName(), job.getTemplateDstName(), job.getTemplateDstTwoName(), job.getSrcCommd(), job.getDstCommd(), job.getDstTwoCommd());

        JdbcTemplate jdbcTemplateSrc = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateSrcName());
        String url = job.getTemplateDstName();
        JdbcTemplate jdbcTemplateTwoDst = (JdbcTemplate) SpringContextUtil.getBean(job.getTemplateDstTwoName());
        String srcCmd = job.getSrcCommd();
        String dstCmd = job.getDstCommd();
        String dstTwoCmd = job.getDstTwoCommd();


        HashMap<String, String> arg = new HashMap<String, String>();
        String[] s = dstCmd.split("\n");
        for (String ss : s) {
            String[] ag = ss.split("\\|");
            arg.put(ag[0], ag[1]);
        }

        List<Map<String, Object>> srcData = jdbcTemplateSrc.queryForList(srcCmd);
        if (srcData.size() > 0) {
            List<Object[]> listDstTwo = new ArrayList<Object[]>();
            for (Map<String, Object> mp : srcData) {
                String rmsg = null;
                System.out.println(job.getTransType() + "   " + url + "   " + mp.get("msg").toString());
                if (job.getTransType().equals("1")) {
                    //post
                    if (url.startsWith("https")) {
                        rmsg = HttpsUtil.sendPost(url, mp.get("msg").toString(), arg);
                    } else {
                        rmsg = HttpUtil.sendArgsPost(url, mp.get("msg").toString(), arg);
                    }
                } else {
                    //get
                    if (url.startsWith("https")) {
                        rmsg = HttpsUtil.sendGet(url, mp.get("msg").toString(), arg);
                    } else {
                        rmsg = HttpUtil.sendArgsGet(url, mp.get("msg").toString(), arg);
                    }
                }
                log.info(job.getTransType() + "   " + url + "   " + mp.get("msg").toString() + " " + rmsg);
                listDstTwo.add(new String[]{rmsg, mp.get("id").toString()});
            }

            if (!(dstTwoCmd == null || dstTwoCmd.equals(""))) {
                int[] ans = jdbcTemplateTwoDst.batchUpdate(dstTwoCmd, listDstTwo);
            }
        }
        log.info("{}-{}----end", job.getRemark(), "httpSend");
    }
}
